﻿
Imports LiarsDice.AI
Imports LiarsDice.CommonHand
Imports System.Windows.Controls.Primitives

Class MainWindow
    Private WithEvents _game As New GameEngine()
    Private _playerToMakeBid As PlayerBase

    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

    End Sub

    Private Sub menuNewGame_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles menuNewGame.Click
        If (_game IsNot Nothing) Then
            _game.Dispose()
        End If


        _game = New GameEngine()
    End Sub

    Private Sub menuAddPlayer_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles menuAddPlayer.Click
        Dim humanPlayer As New CommonHandHumanPlayer()

        humanPlayer.Name = "Human"
        AddHandler humanPlayer.TimeToMakeABid, AddressOf TimeToMakeBid

        AddPlayer(humanPlayer)
    End Sub

    Private Sub TimeToMakeBid(ByVal player As PlayerBase, ByVal previousBid As Bid)
        MessageBox.Show("Your turn. Select bid values and post your bid.")

        _playerToMakeBid = player

        ShowDicesForPlayer(_playerToMakeBid)
    End Sub

    Private Sub ShowDicesForPlayer(ByVal playerToMakeBid As PlayerBase)
        dicesLabel.Content = ""
        For Each i In playerToMakeBid.HiddenDices
            dicesLabel.Content += " " & i
        Next
    End Sub

    Private Sub menuStartGame_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles menuStartGame.Click
        If (_game IsNot Nothing) Then

            _game.Start()

        End If
    End Sub

    Private Sub menuAddAI_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles menuAddAI.Click
        AddPlayer(New SimpleAIPlayer())
    End Sub

    Private Sub AddPlayer(ByVal player As PlayerBase)
        If (_game IsNot Nothing) Then
            If (Not _game.AddPlayer(player)) Then
                DisplayGameFullMessage()
            End If

        End If
    End Sub

    Private Sub DisplayGameFullMessage()
        MessageBox.Show("The game is full. The player could not be added to the current game.", "Game Full")
    End Sub

    Private Sub game_BidMade(ByVal player As PlayerBase, ByVal bid As Bid) Handles _game.BidMade
        If (bid.BidType = BidType.Raise) Then
            roundBidHistoryListBox.Items.Add(String.Format("{0}: {1} {2}s", player.Name, bid.BidValue.DieCount, bid.BidValue.DieValue))
        Else
            roundBidHistoryListBox.Items.Add(String.Format("{0}: Challenge!", player.Name))
        End If

        currentBidLabel.Content = bid.ToString()
    End Sub

    Private Sub _game_GameStarted(ByVal game As GameEngine) Handles _game.GameStarted
        If (_dieCountButtons IsNot Nothing) Then
            For Each tb In _dieCountButtons
                RemoveHandler tb.Checked, AddressOf dieCountButtons_Checked

                dieCountButtonDockPanel.Children.Remove(tb)
            Next

            _dieCountButtons.Clear()
        End If


        For index = 1 To game.PlayerCount * game.NumberOfDicesPerPlayer

            Dim tb As New ToggleButton()
            tb.Content = index.ToString()
            dieCountButtonDockPanel.Children.Add(tb)
            _dieCountButtons.Add(tb)

            AddHandler tb.Checked, AddressOf dieCountButtons_Checked
        Next

    End Sub



    Private Sub game_NewRoundStarting(ByVal round As Integer) Handles _game.NewRoundStarting
        MessageBox.Show(String.Format("Round {0} is about to start.", round))
    End Sub

    Private Sub game_PlayerJoined(ByVal player As PlayerBase) Handles _game.PlayerJoined
        playersListBox.Items.Add(player.Name)
    End Sub

    Private Sub menuExit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles menuExit.Click
        If (_game IsNot Nothing) Then
            _game.Dispose()
        End If

        Application.Current.Shutdown()
    End Sub

    Private Sub makeBidButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles makeBidButton.Click
        If (bidTypeChallengeRB.IsChecked) Then
            _playerToMakeBid.MakeBid(New Bid(BidType.Challenge, Nothing))
        Else
            _playerToMakeBid.MakeBid(New Bid(BidType.Raise, New CommonHandBidValue(SelectedDieValue, SelectedDieCount)))
        End If

    End Sub

    Private Sub dieCountButtons_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
        For Each tb As ToggleButton In _dieCountButtons
            If (Not Object.ReferenceEquals(sender, tb)) Then
                tb.IsChecked = False
            End If
        Next

        _checkedDieCountButton = sender

    End Sub

    Private Sub dieValueButtons_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles dieValue2Button.Checked, dieValue3Button.Checked, dieValue4Button.Checked, dieValue5Button.Checked, dieValue6Button.Checked
        If (_dieValueButtons Is Nothing) Then
            _dieValueButtons = New List(Of ToggleButton)


            _dieValueButtons.AddRange({dieValue2Button, dieValue3Button, dieValue4Button, dieValue5Button, dieValue6Button})
        End If


        For Each tb As ToggleButton In _dieValueButtons
            If (Not Object.ReferenceEquals(sender, tb)) Then
                tb.IsChecked = False
            End If
        Next

        _checkedDieValueButton = sender
    End Sub

    Private ReadOnly Property SelectedDieValue() As Integer
        Get
            Return _checkedDieValueButton.Content
        End Get
    End Property

    Private ReadOnly Property SelectedDieCount() As Integer
        Get
            Return _checkedDieCountButton.Content
        End Get
    End Property

    Private _checkedDieValueButton As ToggleButton
    Private _dieValueButtons As List(Of ToggleButton)

    Private _checkedDieCountButton As ToggleButton
    Private _dieCountButtons As New List(Of ToggleButton)

End Class
